﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OpenQuarters.WebQuarters.Utils;
using System.Xml;
using System.Data.Linq;

namespace OpenQuarters.WebQuarters.Core
{
    public partial class CMSPageTemplate : CMSEntity<CMSPageTemplate, int>
    {
        [Newtonsoft.Json.JsonProperty]
        public int json_PageTemplateId
        {
            get { return this.PageTemplateId; }
            set { this.PageTemplateId = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        public string json_Title
        {
            get { return this.Title; }
            set { this.Title = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        public string json_Template
        {
            get { return this.Template; }
            set { this.Template = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        public string json_Keywords
        {
            get { return this.Keywords; }
            set { this.Keywords = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        public string json_Description
        {
            get { return this.Description; }
            set { this.Description = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        public string json_Tags
        {
            get { return this.Tags; }
            set { this.Tags = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        public bool json_NoFollow
        {
            get { return this.NoFollow; }
            set { this.NoFollow = value; }
        }

        public static List<CMSPageTemplate> Templates
        {
            get
            {
                return CMSPageTemplate.Table.ToList();
            }
        }

        partial void OnTemplateChanging(string value)
        {
            if (String.IsNullOrEmpty(value))
            {
                string sError = "Template cannot be blank";
                throw new Exception(sError);
            }
        }

        public string[] GetTags()
        {
            if (string.IsNullOrEmpty(this.Tags))
                return new string[]{};
            else
                return this.Tags.Replace(" ,", "").Replace(", ", "").Replace("  ", " ").Split(',');
        }

        partial void OnCreated()
        {
            this.LanguageId = Language.Current.LanguageId;
            this.Template = "Default";
        }

        private ContentDefinition contentDefinition = null;

        public ContentDefinition ContentDefinition
        {
            get
            {
                if (contentDefinition == null)
                {
                    contentDefinition = ContentDefinition.FromJSON(this.Definition);
                }
                return contentDefinition;
            }
            set
            {
                contentDefinition = value;
            }
        }

        public override void OnSaving()
        {
            base.OnSaving();
            this.Definition = this.ContentDefinition.ToJSON();
        }

        public override System.Linq.Expressions.Expression<Func<CMSPageTemplate, bool>> GetIDSelector(int ID)
        {
            return e => e.PageTemplateId == ID;
        }

        public override int GetKey(CMSPageTemplate Entity)
        {
            return Entity.PageTemplateId;
        }

        public class JSON
        {
            public CMSPageTemplate PageTemplate = null;
            public string Definition = "{}";

            public JSON(CMSPageTemplate PageTemplate)
            {
                this.PageTemplate = PageTemplate;
                this.Definition = PageTemplate.Definition;
            }

            public JSON()
            {
            }
        }

    }
}
